﻿using Infov1.Commands;
using Infov1.Views;
using System.Windows;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;


namespace Infov1.ViewModels
{
    public class IndexViewModel : ViewModelBase
    {
        //绑定管理员的姓名
        private string _adminName;
        public string AdminName
        {
            get => _adminName;
            set
            {
                _adminName = value;
                OnPropertyChanged();
            }
        }
        // 存储所有打开的标签页集合
        private ObservableCollection<TabItem> _tabItems;
            public ObservableCollection<TabItem> TabItems
            {
                get => _tabItems;
                set => SetProperty(ref _tabItems, value);
            }
        // 当前选中的标签页
            private TabItem _selectedTab;
            public TabItem SelectedTab
            {
                get => _selectedTab;
                set => SetProperty(ref _selectedTab, value);
            }
        // 导航命令，用于处理菜单点击
            public ICommand NavigateCommand { get; }

            public IndexViewModel()
            {
                            ///初始化管理员名字
                            if (Application.Current.Properties["Name"] != null)
                            {
                                AdminName = Application.Current.Properties["Name"].ToString();
                            }
                            else
                            {
                                AdminName = "未登录";
                            }
            // 初始化标签页集合
            TabItems = new ObservableCollection<TabItem>();

            // 初始化导航命令，将 Navigate 方法绑定到命令
            NavigateCommand = new RelayCommand<string>(Navigate);

            // 创建并添加默认的首页标签
            var homeTab = new HandyControl.Controls.TabItem
            {
                Header = "首页",
                Content = new HomeView(),
                ShowCloseButton = false,  // 首页不可关闭
            };
            TabItems.Add(homeTab);
            SelectedTab = homeTab;  // 设置为当前选中
        }

            private void Navigate(string parameter)
            {
                // 检查是否已存在相同标题的标签页
                var existingTab = TabItems.FirstOrDefault(t => t.Header.ToString() == GetTabHeader(parameter));
                if (existingTab != null)
                {
                    SelectedTab = existingTab;
                    return;
                }

                // 创建新标签页
                var newTab = new HandyControl.Controls.TabItem
                {
                    Header = GetTabHeader(parameter),
                    Content = CreateView(parameter),
                    ShowCloseButton = true,                   
                };

                TabItems.Add(newTab);
                SelectedTab = newTab;
            }

            private string GetTabHeader(string parameter)
            {
                switch (parameter)
                {
                    case "Home":
                        return "首页";
                    case "CustomerAreaView":
                        return "客户区域";
                    case "CustomerCategory":
                        return "客户类别";
                    case "CustomerInfoView":
                        return "客户档案";
                case "WorkRecordView":
                    return "工作登记";
                case "WorkStatistics":
                    return "工作报表";
                default:
                        return parameter;
                }
            }

            private UserControl CreateView(string parameter)
            {
                switch (parameter)
                {
                 case "Home":
                        return new HomeView();
                case "CustomerAreaView":
                    return new CustomerAreaView();
                case "CustomerCategory":
                    return new CustomerCategory();
                case "CustomerInfoView":
                    return new Infov1.Views.CustomerInfoView();
                case "WorkRecordView":
                    return new Infov1.Views.WorkRecord.WorkRecordView();
                case "WorkStatistics":
                    return new Infov1.Views.WorkForm.WorkStatistics();
                default:
                        return new HomeView();
                }
            }
     }

    


}
